package top.revery.task.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import top.revery.api.entity.task.BaseTask;
import top.revery.task.dto.TaskDto;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Map;

/**
 * @author denghao
 * @title
 * @package
 * @date 2019/12/14 9:40
 */
@Service
public class TaskService {
    @Autowired
    private TaskDto taskDto;

    public Page<BaseTask> findPage(int pageNum, int pageSize, Map map) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "pubT");
        Specification<BaseTask> specification = createSpecification(map);
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.by(order));
        return taskDto.findAll(specification, pageRequest);
    }

    private Specification<BaseTask> createSpecification(Map map) {

        return new Specification<BaseTask>() {
            @Override
            public Predicate toPredicate(Root<BaseTask> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                //id
                if (map.get("id") != null && !"".equals(map.get("id"))) {
                    predicate.getExpressions().add(cb.equal(root.get("id"), map.get("id")));
                }
                //任务类型
                if (map.get("taskType") != null && !"".equals(map.get("taskType"))) {
                    predicate.getExpressions().add(cb.like(root.get("taskType"), "%"+(String) map.get("taskType")+"%"));
                }
                //任务标签
                if (map.get("label") != null && !"".equals(map.get("label"))) {
                    predicate.getExpressions().add(cb.equal(root.get("label"), map.get("label")));
                }
                //任务名称
                if (map.get("taskName") != null && !"".equals(map.get("taskName"))) {
                    predicate.getExpressions().add(cb.like(root.get("taskName"), "%"+(String) map.get("taskName")+"%"));
                }
               //任务是否发布
                if (map.get("isfo") != null && !"".equals(map.get("isfo"))) {
                    predicate.getExpressions().add(cb.equal(root.get("isfo"), map.get("isfo")));
                }

                //任务状态
                if (map.get("status") != null && !"".equals(map.get("status"))) {
                    predicate.getExpressions().add(cb.equal(root.get("status"), map.get("status")));
                }
                //发布日期查询
                if (map.get("startTime") != null && !"".equals(map.get("startTime"))) {
                    predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("pubT").as(String.class), (String) map.get("startTime")));
                }
                if (map.get("endTime") != null && !"".equals(map.get("endTime"))) {
                    predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("pubT").as(String.class), (String) map.get("endTime")));
                }
                return predicate;
            }
        };
    }
}
